home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.1.5 / components / nsXULAppInstall.js < prev    next >
Text File  |  2009-11-09  |  8KB  |  301 lines

  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. //@line 41 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/xulrunner/setup/nsXULAppInstall.js"
  3.  
  4. const nsIFile             = Components.interfaces.nsIFile;
  5. const nsIINIParser        = Components.interfaces.nsIINIParser;
  6. const nsIINIParserFactory = Components.interfaces.nsIINIParserFactory;
  7. const nsILocalFile        = Components.interfaces.nsILocalFile;
  8. const nsISupports         = Components.interfaces.nsISupports;
  9. const nsIXULAppInstall    = Components.interfaces.nsIXULAppInstall;
  10. const nsIZipReader        = Components.interfaces.nsIZipReader;
  11.  
  12. function getDirectoryKey(aKey) {
  13.   try {
  14.     return Components.classes["@mozilla.org/file/directory_service;1"].
  15.       getService(Components.interfaces.nsIProperties).
  16.       get(aKey, nsIFile);
  17.   }
  18.   catch (e) {
  19.     throw "Couln't get directory service key: " + aKey;
  20.   }
  21. }
  22.  
  23. function createINIParser(aFile) {
  24.   return Components.manager.
  25.     getClassObjectByContractID("@mozilla.org/xpcom/ini-parser-factory;1",
  26.                                nsIINIParserFactory).
  27.     createINIParser(aFile);
  28. }
  29.  
  30. function copy_recurse(aSource, aDest) {
  31.   var e = aSource.directoryEntries;
  32.  
  33.   while (e.hasMoreElements()) {
  34.     var f = e.getNext().QueryInterface(nsIFile);
  35.     var leaf = f.leafName;
  36.  
  37.     var ddest = aDest.clone();
  38.     ddest.append(leaf);
  39.  
  40.     if (f.isDirectory()) {
  41.       copy_recurse(f, ddest);
  42.     }
  43.     else {
  44.       if (ddest.exists())
  45.         ddest.remove(false);
  46.  
  47.       f.copyTo(aDest, leaf);
  48.     }
  49.   }
  50. }
  51.  
  52. const PR_WRONLY = 0x02;
  53. const PR_CREATE_FILE = 0x08;
  54. const PR_TRUNCATE = 0x20;
  55.  
  56. function openFileOutputStream(aFile) {
  57.   var s = Components.classes["@mozilla.org/network/file-output-stream;1"].
  58.     createInstance(Components.interfaces.nsIFileOutputStream);
  59.   s.init(aFile, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0644, 0);
  60.   return s;
  61. }
  62.  
  63. /**
  64.  * An extractor implements the following prototype:
  65.  * readonly attribute nsIINIPaser iniParser;
  66.  * void copyTo(in nsILocalFile root);
  67.  */
  68.  
  69. function directoryExtractor(aFile) {
  70.   this.mDirectory = aFile;
  71. }
  72.  
  73. directoryExtractor.prototype = {
  74.   mINIParser : null,
  75.  
  76.   get iniParser() {
  77.     if (!this.mINIParser) {
  78.       var iniFile = this.mDirectory.clone();
  79.       iniFile.append("application.ini");
  80.  
  81.       this.mINIParser = createINIParser(iniFile);
  82.     }
  83.     return this.mINIParser;
  84.   },
  85.  
  86.   copyTo : function de_copyTo(aDest) {
  87.     // Assume the root already exists
  88.     copy_recurse(this.mDirectory, aDest);
  89.   }
  90. };
  91.  
  92. function zipExtractor(aFile) {
  93.   this.mZipReader = Components.classes["@mozilla.org/libjar/zip-reader;1"].
  94.     createInstance(nsIZipReader);
  95.   this.mZipReader.open(aFile);
  96.   this.mZipReader.test(null);
  97. }
  98.  
  99. zipExtractor.prototype = {
  100.   mINIParser : null,
  101.  
  102.   get iniParser() {
  103.     if (!this.mINIParser) {
  104.       // XXXbsmedberg: this is not very unique, guessing could be a problem
  105.       var f = getDirectoryKey("TmpD");
  106.       f.append("application.ini");
  107.       f.createUnique(nsIFile.NORMAL_FILE_TYPE, 0600);
  108.  
  109.       try {
  110.         this.mZipReader.extract("application.ini", f);
  111.         this.mINIParser = createINIParser(f);
  112.       }
  113.       catch (e) {
  114.         try {
  115.           f.remove();
  116.         }
  117.         catch (ee) { }
  118.  
  119.         throw e;
  120.       }
  121.       try {
  122.         f.remove();
  123.       }
  124.       catch (e) { }
  125.     }
  126.     return this.mINIParser;
  127.   },
  128.  
  129.   copyTo : function ze_CopyTo(aDest) {
  130.     var entries = this.mZipReader.findEntries(null);
  131.     while (entries.hasMore()) {
  132.       var entryName = entries.getNext();
  133.  
  134.       this._installZipEntry(this.mZipReader, entryName, aDest);
  135.     }
  136.   },
  137.  
  138.   _installZipEntry : function ze_installZipEntry(aZipReader, aZipEntry,
  139.                                                  aDestination) {
  140.     var file = aDestination.clone();
  141.  
  142.     var dirs = aZipEntry.split(/\//);
  143.     var isDirectory = /\/$/.test(aZipEntry);
  144.  
  145.     var end = dirs.length;
  146.     if (!isDirectory)
  147.       --end;
  148.  
  149.     for (var i = 0; i < end; ++i) {
  150.       file.append(dirs[i]);
  151.       if (!file.exists()) {
  152.         file.create(nsIFile.DIRECTORY_TYPE, 0755);
  153.       }
  154.     }
  155.  
  156.     if (!isDirectory) {
  157.       file.append(dirs[end]);
  158.       aZipReader.extract(aZipEntry, file);
  159.     }
  160.   }
  161. };
  162.  
  163. function createExtractor(aFile) {
  164.   if (aFile.isDirectory())
  165.     return new directoryExtractor(aFile);
  166.  
  167.   return new zipExtractor(aFile);
  168. }
  169.  
  170. const AppInstall = {
  171.  
  172.   /* nsISupports */
  173.   QueryInterface : function ai_QI(iid) {
  174.     if (iid.equals(nsIXULAppInstall) ||
  175.         iid.equals(nsISupports))
  176.       return this;
  177.  
  178.     throw Components.result.NS_ERROR_NO_INTERFACE;
  179.   },
  180.  
  181.   /* nsIXULAppInstall */
  182.   installApplication : function ai_IA(aAppFile, aDirectory, aLeafName) {
  183.     var extractor = createExtractor(aAppFile);
  184.     var iniParser = extractor.iniParser;
  185.  
  186.     var appName = iniParser.getString("App", "Name");
  187.  
  188.     // vendor is optional
  189.     var vendor;
  190.     try {
  191.       vendor = iniParser.getString("App", "Vendor");
  192.     }
  193.     catch (e) { }
  194.  
  195.     if (aDirectory == null) {
  196. //@line 244 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/xulrunner/setup/nsXULAppInstall.js"
  197.       aDirectory = Components.classes["@mozilla.org/file/local;1"].
  198.         createInstance(nsILocalFile);
  199.       aDirectory.initWithPath("/usr/lib");
  200.       if (vendor)
  201.         aDirectory.append(vendor.toLowerCase());
  202. //@line 251 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/xulrunner/setup/nsXULAppInstall.js"
  203.     }
  204.     else {
  205.       aDirectory = aDirectory.clone();
  206.     }
  207.  
  208.     if (!aDirectory.exists()) {
  209.       aDirectory.create(nsIFile.DIRECTORY_TYPE, 0755);
  210.     }
  211.  
  212.     if (aLeafName == "") {
  213. //@line 267 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/xulrunner/setup/nsXULAppInstall.js"
  214.       aLeafName = appName.toLowerCase();
  215. //@line 270 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/xulrunner/setup/nsXULAppInstall.js"
  216.     }
  217.  
  218.     aDirectory.append(aLeafName);
  219.     if (!aDirectory.exists()) {
  220.       aDirectory.create(nsIFile.DIRECTORY_TYPE, 0755);
  221.     }
  222.  
  223. //@line 341 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/xulrunner/setup/nsXULAppInstall.js"
  224.     extractor.copyTo(aDirectory);
  225.  
  226.     var xulrunnerBinary = getDirectoryKey("XCurProcD");
  227.     xulrunnerBinary.append("xulrunner-stub");
  228.  
  229.     xulrunnerBinary.copyTo(aDirectory, appName.toLowerCase() + "");
  230. //@line 348 "/build/buildd/xulrunner-1.9.1-1.9.1.5+nobinonly/build-tree/mozilla/xulrunner/setup/nsXULAppInstall.js"
  231.   }
  232. };
  233.  
  234. const AppInstallFactory = {
  235.   /* nsISupports */
  236.   QueryInterface : function aif_QI(iid) {
  237.     if (iid.equals(Components.interfaces.nsIFactory) ||
  238.         iid.equals(nsISupports))
  239.       return this;
  240.  
  241.     throw Components.results.NS_ERROR_NO_INTERFACE;
  242.   },
  243.  
  244.   /* nsIFactory */
  245.   createInstance : function aif_CI(aOuter, aIID) {
  246.     if (aOuter)
  247.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  248.  
  249.     return AppInstall.QueryInterface(aIID);
  250.   },
  251.  
  252.   lockFactory : function aif_lock(aLock) { }
  253. };
  254.  
  255. const AppInstallContractID = "@mozilla.org/xulrunner/app-install-service;1";
  256. const AppInstallCID = Components.ID("{00790a19-27e2-4d9a-bef0-244080feabfd}");
  257.  
  258. const AppInstallModule = {
  259.   /* nsISupports */
  260.   QueryInterface : function mod_QI(iid) {
  261.     if (iid.equals(Components.interfaces.nsIModule) ||
  262.         iid.equals(nsISupports))
  263.       return this;
  264.  
  265.     throw Components.results.NS_ERROR_NO_INTERFACE;
  266.   },
  267.  
  268.   /* nsIModule */
  269.   getClassObject : function mod_gco(aCompMgr, aClass, aIID) {
  270.     if (aClass.equals(AppInstallCID))
  271.       return AppInstallFactory.QueryInterface(aIID);
  272.  
  273.     return Components.results.NS_ERROR_FACTORY_NOT_REGISTERED;
  274.   },
  275.  
  276.   registerSelf : function mod_regself(aCompMgr, aLocation,
  277.                                       aLoaderStr, aType) {
  278.     var reg = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  279.     reg.registerFactoryLocation(AppInstallCID,
  280.                                 "nsXULAppInstall",
  281.                                 AppInstallContractID,
  282.                                 aLocation,
  283.                                 aLoaderStr,
  284.                                 aType);
  285.   },
  286.  
  287.   unregisterSelf : function mod_unreg(aCompMgr, aLocation, aLoaderStr) {
  288.     var reg = aCompMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  289.     reg.unregisterFactoryLocation(AppInstallCID,
  290.                                   aLocation);
  291.   },
  292.  
  293.   canUnload : function mod_unload(aCompMgr) {
  294.     return true;
  295.   }
  296. };
  297.  
  298. function NSGetModule(compMgr, fileSpec) {
  299.   return AppInstallModule;
  300. }
  301.